package com.gene.controller.item;

import com.gene.pojo.TbItemCat;
import com.gene.service.ITbitmeCarService;
import com.gene.utils.CodeValue;
import com.gene.utils.MsgValue;
import com.gene.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by lenovo on 2018/1/15.
 * 商品分类
 */
@Controller
@RequestMapping("/gene-safe/itemCar")
public class ItemCarController {

    @Autowired
    private ITbitmeCarService tbitmeCarService;

    /**
     * 查询目录
     * @return
     */
    @RequestMapping(value="/query",method = RequestMethod.POST)
    @ResponseBody
    public ResultData query(@RequestParam(defaultValue = "1")Long parentId){
        ResultData data=new ResultData();
        List<TbItemCat> query = tbitmeCarService.query(parentId);
        data.setData(query);
        return data;
    }
    /**
     * 添加商品类目
     *
     */
    @RequestMapping(value="/add",method = RequestMethod.POST)
    @ResponseBody
    public ResultData add(Long parentId,String name){
        ResultData result=new ResultData();
        if(null==parentId||name==null||name.equals("")||name.equals("null")){
            result.setErrorCode(CodeValue.USER_PARAMETERS_ISNULL);
            result.setMsg(MsgValue.USER_PARAMETERS_ISNULL);
            return result;
        }
        TbItemCat add = tbitmeCarService.add(parentId, name);
        result.setData(add);
        result.setErrorCode(CodeValue.SUCCESS);
        result.setMsg(MsgValue.SUCCESS);
        return result;
    }
    /**
     * 修改商品类目
     */
    @RequestMapping(value="/edit",method = RequestMethod.POST)
    @ResponseBody
    public ResultData edit(TbItemCat cat){
        ResultData result=new ResultData();
        if(null==cat){
            result.setErrorCode(CodeValue.USER_PARAMETERS_ISNULL);
            result.setMsg(MsgValue.USER_PARAMETERS_ISNULL);
            return result;
        }
        TbItemCat edit = tbitmeCarService.edit(cat);
        result.setData(edit);
        result.setErrorCode(CodeValue.SUCCESS);
        result.setMsg(MsgValue.SUCCESS);
        return result;
    }
    /**
     * 删除商品类目
     * delete
     */
    @RequestMapping(value="/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResultData edit(Long id){
        ResultData result=new ResultData();
        if(null==id){
            result.setErrorCode(CodeValue.USER_PARAMETERS_ISNULL);
            result.setMsg(MsgValue.USER_PARAMETERS_ISNULL);
            return result;
        }
        tbitmeCarService.delete(id);
        result.setErrorCode(CodeValue.SUCCESS);
        result.setMsg(MsgValue.SUCCESS);
        return result;
    }



    /**
     * 查询商品二级目录  isParent=false    status=1
     * @return
     */
    @RequestMapping(value="/select")
    @ResponseBody
    public ResultData queryTwo(){
        ResultData result=new ResultData();
        List<TbItemCat> list = tbitmeCarService.twoQuery();
        if(list.size()>0){
            result.setData(list);
            result.setStatus("1");
            result.setMsg("查询成功");
        }else{
            result.setStatus("0");
            result.setMsg("查询失败");
        }

        return result;
    }
}
